//
//  HMToolBar.swift
//  weibo28
//
//  Created by 范英强 on 16/3/25.
//  Copyright © 2016年 范英强. All rights reserved.
//

import UIKit

class HMToolBar: UIView {
    
    var retweetButton: UIButton?
    var commentButton: UIButton?
    var unlikeButton: UIButton?
    
    var status: HMStatus?{
        didSet{
            // 产品需求 
            // 如果是0 的话 显示文字 显示转发评论赞
            // 如果 转发 评论 赞  小于 10000 大于 0  那么是多少就显示多少
            // 如果大于10000 且 小于 11000  那么久显示 1万
            // 如果 大于等于11000  就显示 1.1万
            // 20500  == 2万   22345 显示 2.2万
            
            let repostsCountStr = getButtonTitleCount(status?.reposts_count ?? 0, title: "转发")
            
            retweetButton?.setTitle(repostsCountStr, forState: UIControlState.Normal)
            let commentsCountStr = getButtonTitleCount(status?.comments_count ?? 0, title: "评论")
            commentButton?.setTitle(commentsCountStr, forState: UIControlState.Normal)
            let attitudesCountStr = getButtonTitleCount(status?.attitudes_count ?? 0, title: "赞")
            unlikeButton?.setTitle(attitudesCountStr, forState: UIControlState.Normal)
            
            
            
            
            
        }
    }
    
    func getButtonTitleCount(count: Int, title: String) -> String{
        var countString = title
        
        if count <= 0{
            return countString
        }else if (count > 0 && count < 10000){
            return "\(count)"
        }else{
  
            let c = CGFloat(count / 1000) / 10
            countString = "\(c)万"
            
            if countString.containsString(".0"){
               countString = countString.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return countString
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
         setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
   
        // 01- 控件的添加
        retweetButton = addButton("timeline_icon_retweet", title: "转发")
        commentButton = addButton("timeline_icon_comment", title: "评论")
        unlikeButton = addButton("timeline_icon_unlike", title: "赞")
        let sp1 = addLine()
        let sp2 = addLine()
        
        // 02- 给控件添加约束
        retweetButton?.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.bottom.equalTo(self)
            make.width.equalTo(commentButton!)
        }
        
        commentButton?.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(self)
            make.leading.equalTo(retweetButton!.snp_trailing)
            make.width.equalTo(unlikeButton!)
        }
        
        unlikeButton?.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.leading.equalTo(commentButton!.snp_trailing)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton!.snp_trailing)
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton!.snp_trailing)
            make.centerY.equalTo(self)
        }

    }
    // 添加竖线
    func addLine() -> UIView{
        let line = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(line)
        return line
    }
    
    
    // 添加按钮
    func addButton(imgName: String,title: String) -> UIButton{
        //timeline_icon_retweet
        
        let button = UIButton()
        // 设置背景图片
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        // 设施image
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        // 设置文字
        button.setTitle(title, forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.sizeToFit()
        addSubview(button)
        
        return button
    }
    
    

    
    
    
}
